using System;
using System.Collections.Generic;

using Csla;

using EntModApp.EntServices.Business;

using DemoApp.DataAccess;

namespace DemoApp.BusinessLayer 
{
  public partial class CslaSecurableActionInfo : 
    ReadOnlyBase<CslaSecurableActionInfo>,
    ISecurableAction
  {
    #region [ Business Properties ]

    public static PropertyInfo<Guid> SecurableActionIdPropertyInfo = RegisterProperty(new PropertyInfo<Guid>("SecurableActionId"));
    public Guid SecurableActionId 
    { 
      get
      {
        return ReadProperty(SecurableActionIdPropertyInfo);
      }
      private set
      {
        LoadProperty(SecurableActionIdPropertyInfo, value);
      }
    }

    public static PropertyInfo<string> NamePropertyInfo = RegisterProperty(new PropertyInfo<string>("Name"));
    public string Name 
    { 
      get
      {
        return ReadProperty(NamePropertyInfo);
      }
      private set
      {
        LoadProperty(NamePropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaRoleInfoCollection> RolesPropertyInfo = RegisterProperty(new PropertyInfo<CslaRoleInfoCollection>("Roles"));
    public CslaRoleInfoCollection Roles 
    { 
      get
      {
        return ReadProperty(RolesPropertyInfo);
      }
      private set
      {
        LoadProperty(RolesPropertyInfo, value);
      }
    }

    #endregion [ Business Properties ]

    #region [ Factory Methods ]
    protected internal static CslaSecurableActionInfo GetSecurableActionInfo(SecurableAction data, SecurableActionLoadSettings loadSettings)
    {
      if (data == null) throw new ArgumentNullException("data");
      if (loadSettings == null) throw new ArgumentNullException("loadSettings");
      CslaSecurableActionInfo result = new CslaSecurableActionInfo();
      result.FetchSecurableAction(data, loadSettings);
      return result;
    }

    private static int ForceInitField = 0;
    protected CslaSecurableActionInfo() 
    {
      ForceInitField = 1;
    }
    #endregion [ Factory Methods ]

    #region [ Data Access ]
    protected void FetchSecurableAction(SecurableAction data, SecurableActionLoadSettings loadSettings)
    {
      FetchSecurableActionData(data, loadSettings);
      FetchSecurableActionChildren(data, loadSettings);
    }

    protected void FetchSecurableActionData(SecurableAction data, SecurableActionLoadSettings loadSettings)
    {
      this.SecurableActionId = data.SecurableActionId;
      this.Name = data.Name;
    }

    protected void FetchSecurableActionChildren(SecurableAction data, SecurableActionLoadSettings loadSettings)
    {
      if (loadSettings.Inclusion.IncludeRoles)
        this.Roles = CslaRoleInfoCollection.GetRoleInfoCollection(data.Roles, loadSettings.Inclusion.IncludedRolesSettings);
    }
    #endregion [ Data Access ]

    #region [ Explicit ISecurableAction implementation ]

    Guid ISecurableAction.SecurableActionId
    {
      get { return this.SecurableActionId; }
    }

    string ISecurableAction.Name
    {
      get { return this.Name; }
      set
      {
        throw new NotSupportedException();
      }
    }

    IBusinessObjectCollection<IRole> ISecurableAction.Roles
    { 
      get
      {
        return this.Roles;
      }
    }


    bool IBusinessObject.IsReadonly
    {
      get { return true; }
    }

    bool IBusinessObject.IsDirty
    {
      get { return false; }
    }

    #endregion [ Explicit ISecurableAction implementation ]
  }
}
